﻿#include "SetConfigCmd.h"
#include "../util/JsonUtil.h"
#include "../common/Common.h"

SetConfigCmd::SetConfigCmd()
{
}

SetConfigCmd::~SetConfigCmd()
{
}

bool SetConfigCmd::Parse(std::string json)
{
	// 解析json
	JsonUtil jsonUtil;
	if (jsonUtil.Parse(json))
	{
		m_configs.clear();

		int saveDays = jsonUtil.GetInt("config.saveDays", 1);
		ConfigInfo item;
		item.SetKey(CONFIG_TYPE_SAVE_DAYS);
		item.SetParams(std::to_string(saveDays));
		m_configs.push_back(item);

		int concurrency = jsonUtil.GetInt("config.concurrency", 8);
		item.SetKey(CONFIG_TYPE_PATROL_NUM);
		item.SetParams(std::to_string(concurrency));
		m_configs.push_back(item);

		int showLabel = jsonUtil.GetInt("config.showLabel", 1);
		item.SetKey(CONFIG_TYPE_SHOW_LABEL);
		item.SetParams(std::to_string(showLabel));
		m_configs.push_back(item);

		int enableRecord = jsonUtil.GetInt("config.enableRecord", 1);
		item.SetKey(CONFIG_TYPE_ALARM_RECORD);
		item.SetParams(std::to_string(enableRecord));
		m_configs.push_back(item);

		int autoRestart = jsonUtil.GetInt("config.autoRestart", 1);
		item.SetKey(CONFIG_TYPE_AUTO_RESTART);
		item.SetParams(std::to_string(autoRestart));
		m_configs.push_back(item);

		std::string requestUrl = jsonUtil.GetString("config.requestUrl", "");
		item.SetKey(CONFIG_TYPE_ALARM_URL);
		item.SetParams(requestUrl);
		m_configs.push_back(item);

		std::string socketUrl = jsonUtil.GetString("config.socketUrl", "");
		item.SetKey(CONFIG_TYPE_SOCKET_URL);
		item.SetParams(socketUrl);
		m_configs.push_back(item);
		return true;
	}
	return false;
}

std::vector<ConfigInfo> SetConfigCmd::GetConfigs()
{
	return m_configs;
}